package cn.springcloud.alibaba.gateway.filter;

import cn.springcloud.alibaba.core.constant.Constant;
import cn.springcloud.alibaba.feign.rule.GrayRequestContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
@Component
public class GrayRequestFilter implements GlobalFilter, Ordered {

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		HttpHeaders headers = exchange.getRequest().getHeaders();
		String gray = headers.getFirst(Constant.GRAY_TAG);
		GrayRequestContextHolder.setGrayTag(Boolean.TRUE.toString().equals(gray));
		return chain.filter(exchange);
	}

	@Override
	public int getOrder() {
		return Integer.MIN_VALUE + 1;
	}
}
